//
//  YYECourseNoBuyViewController.m
//  YuYingEDU
//
//  Created by 胡波 on 2018/1/18.
//  Copyright © 2018年 胡波. All rights reserved.
//

#import "YYECourseNoBuyViewController.h"
#import "YYEIntroduceViewController.h"
#import "YYESectionViewController.h"
#import "SPPageMenu.h"
#import "YYECourseBuyViewController.h"
#import <AliyunVodPlayerViewSDK/AliyunVodPlayerViewSDK.h>
#import "YYELoginViewController.h"


@interface YYECourseNoBuyViewController () <SPPageMenuDelegate,UIScrollViewDelegate>

@property (nonatomic,strong) SPPageMenu *pageMenu;
@property (nonatomic,strong) UIScrollView *scrollView;
@property (nonatomic,strong) AliyunVodPlayer *aliPlayer;
@property (nonatomic,strong) UIImageView *backIV;
@property (nonatomic,strong) UILabel *priceLabel;
@property (nonatomic,strong) YYECourseIntroModel *model;

@end

@implementation YYECourseNoBuyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self configUI];
    
    UIButton *backBtn = [[UIButton alloc]init];
    [backBtn setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    [backBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:backBtn];
    
    [backBtn makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.left).offset(12);
        make.top.equalTo(self.view.top).offset(kStatusBarHeight+12);
        make.size.equalTo(CGSizeMake(30, 30));
    }];
    
}

- (void)backAction
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)configUI {
    

    
    
    self.backIV = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_WIDTH * 0.55)];
//    self.backIV.image = [UIImage imageNamed:@"headerBack"];
    [self.view addSubview:self.backIV];
    
    [self.backIV sd_setImageWithURL:[NSURL URLWithString:self.img] placeholderImage:nil];
    
    NSArray *datas = @[@"介绍",@"章节"];
    
    self.pageMenu = [SPPageMenu pageMenuWithFrame:CGRectMake(0, SCREEN_WIDTH * 0.55, SCREEN_WIDTH, 40) trackerStyle:SPPageMenuTrackerStyleLine];
    self.pageMenu.selectedItemTitleColor = GeneralBlueColor;
    self.pageMenu.unSelectedItemTitleColor = LineColor;
    self.pageMenu.tracker.image = [UIImage imageNamed:@"bottomLine"];
    self.pageMenu.dividingLine.backgroundColor = [UIColor whiteColor];
    self.pageMenu.needTextColorGradients = NO;
    [self.pageMenu setWidth:SCREEN_WIDTH / 2 forItemAtIndex:0];
    [self.pageMenu setWidth:SCREEN_WIDTH / 2 forItemAtIndex:1];
    self.pageMenu.delegate = self;
    [self.view addSubview:self.pageMenu];
//    self.pageMenu.backgroundColor = [UIColor redColor];
    
    YYEIntroduceViewController *introVC = [[YYEIntroduceViewController alloc]init];
    introVC.cid = self.cid;
    introVC.block = ^(YYECourseIntroModel *model) {
//        self.model = model;
//        self.priceLabel.text = [NSString stringWithFormat:@"￥%@",model.price];
    };
    
    YYESectionViewController *secVC = [[YYESectionViewController alloc]init];
        secVC.cid = self.cid;
    secVC.isBuy = self.isBuy;
    [self addChildViewController:introVC];
    [self addChildViewController:secVC];
    
    self.scrollView = [[UIScrollView alloc]init];
    self.scrollView.frame = CGRectMake(0, SCREEN_WIDTH * 0.55 + 40, SCREEN_WIDTH, SCREEN_HEIGHT - SCREEN_WIDTH * 0.55 - 40 - 45);

    self.scrollView.delegate = self;
    self.scrollView.pagingEnabled = YES;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    [self.view addSubview:self.scrollView];
    
    [self.scrollView addSubview:introVC.view];
    introVC.view.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - SCREEN_WIDTH * 0.55 - 40 - 45);
    self.scrollView.contentSize = CGSizeMake(SCREEN_WIDTH * 2, 0);
    self.pageMenu.bridgeScrollView = self.scrollView;
    [self.pageMenu setItems:datas selectedItemIndex:0];
    
    [self configBottomView];
    
}

- (void)configBottomView {
    
    UIView *contentView = [[UIView alloc]init];
    contentView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:contentView];
    
    self.priceLabel = [[UILabel alloc]init];
    self.priceLabel.textColor = [UIColor blackColor];
    self.priceLabel.font = [UIFont systemFontOfSize:16];
    self.priceLabel.text = @"本课程如无法观看，请联系专业课老师开通";
    [contentView addSubview:self.priceLabel];
    self.priceLabel.textAlignment = NSTextAlignmentCenter;
    
//    UIButton *payBtn = [[UIButton alloc]init];
//    [payBtn setTitle:@"立即购买" forState:UIControlStateNormal];
//    [payBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//    [payBtn setBackgroundColor:GeneralBlueColor];
//    [payBtn addTarget:self action:@selector(payClicked) forControlEvents:UIControlEventTouchUpInside];
//    [contentView addSubview:payBtn];
    
    [contentView makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.top.equalTo(self.scrollView.bottom);
        make.height.equalTo(45);
    }];
    
    [self.priceLabel makeConstraints:^(MASConstraintMaker *make) {
//        make.left.equalTo(contentView.left).offset(12);
        make.left.right.equalTo(contentView);
        make.centerY.equalTo(contentView.centerY);
    }];
    
//    [payBtn makeConstraints:^(MASConstraintMaker *make) {
//        make.top.bottom.right.equalTo(contentView);
//        make.width.equalTo(107);
//    }];
    
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [self.navigationController setNavigationBarHidden:YES animated:NO];

    
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
    [self.navigationController setNavigationBarHidden:NO animated:NO];

    
}

// 收藏
- (void)collectioClicked {
    
    
    
}

// 购买
- (void)payClicked {
    
    if ([LoginModel shareLogin].token.length == 0) {
        
        YYELoginViewController *vc = [[YYELoginViewController alloc]init];
        [self.navigationController pushViewController:vc animated:YES];
        return;
    }
    
    if ([LoginModel shareLogin].role.integerValue == 1) {

        [self.view makeToast:@"教师不可购买"];
        return;
    }
    
    YYECourseBuyViewController *buyVC = [[YYECourseBuyViewController alloc]init];
    buyVC.cid = self.cid;
    [self.navigationController pushViewController:buyVC animated:YES];
    
}

#pragma mark -- SPPageMenuDelegate
- (void)pageMenu:(SPPageMenu *)pageMenu itemSelectedAtIndex:(NSInteger)index {
    
    self.scrollView.contentOffset = CGPointMake(SCREEN_WIDTH * index, 0);
    
}

- (void)pageMenu:(SPPageMenu *)pageMenu itemSelectedFromIndex:(NSInteger)fromIndex toIndex:(NSInteger)toIndex {
    
    if (labs(toIndex - fromIndex) >= 2) {
        [self.scrollView setContentOffset:CGPointMake(SCREEN_WIDTH * toIndex, 0) animated:NO];
    }else {
        [self.scrollView setContentOffset:CGPointMake(SCREEN_WIDTH * toIndex, 0) animated:YES];
    }
    
    UIViewController *targetViewController = self.childViewControllers[toIndex];
    // 如果已经加载过，就不再加载
    if ([targetViewController isViewLoaded]) return;
    
    targetViewController.view.frame = CGRectMake(SCREEN_WIDTH * toIndex, 0, SCREEN_WIDTH, SCREEN_HEIGHT - SCREEN_WIDTH * 0.55 - 40 - 45);
    [self.scrollView addSubview:targetViewController.view];
    
}

#pragma mark -- UIScrollView
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    NSInteger index = scrollView.contentOffset.x / SCREEN_WIDTH;
    
    UIViewController *targetViewController = self.childViewControllers[index];
    if ([targetViewController isViewLoaded]) return;
    
    self.scrollView.contentOffset = CGPointMake(SCREEN_WIDTH * index, 0);
    
    targetViewController.view.frame = CGRectMake(SCREEN_WIDTH * index, 0, SCREEN_WIDTH, SCREEN_HEIGHT - SCREEN_WIDTH * 0.55 - 40 - 45);
    [self.scrollView addSubview:targetViewController.view];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
